package com.dexilaonian.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.dexilaonian.R;
import com.dexilaonian.ui.BaseActivity;
import com.dexilaonian.utils.Constant;
import com.dexilaonian.utils.PublicUtil;
import com.dexilaonian.utils.WorksSizeCheckUtil;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

import static android.icu.lang.UScript.getCode;

public class RegisterActivity extends BaseActivity {
    @Bind(R.id.back)
    ImageView back;
    @Bind(R.id.title_top)
    TextView titleTop;
    @Bind(R.id.base_login)
    TextView baseLogin;
    @Bind(R.id.rl_title)
    RelativeLayout rlTitle;
    @Bind(R.id.et_input_phone)
    EditText etInputPhone;
    @Bind(R.id.btn_register)
    Button btnRegister;
    @Bind(R.id.rl_xuanze)
    RelativeLayout RLxuanze;
    @Bind(R.id.xuanze)
    ImageView xuanze;
    @Bind(R.id.agreement_cmcc)
    TextView agreementCmcc;

    private int flag = 0;
    private String sms_url;
    private String phone;

    @Override
    protected void initData() {
        sms_url = Constant.base_url + "/verify/sentSmsVerify";
    }

    @Override
    public int getLayoutID() {
        return R.layout.activity_register;
    }

    @Override
    public void initView() {
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        btnRegister.setClickable(false);
        WorksSizeCheckUtil.textChangeListener textChangeListener = new WorksSizeCheckUtil.textChangeListener(btnRegister);
        textChangeListener.addAllEditText(etInputPhone);
        WorksSizeCheckUtil.setChangeListener(new WorksSizeCheckUtil.IEditTextChangeListener() {
            @Override
            public void textChange(boolean isHasContent) {
                if (isHasContent) {
                    btnRegister.setBackground(getResources().getDrawable(R.drawable.button));
                    btnRegister.setClickable(true);
                } else {
                    btnRegister.setBackground(getResources().getDrawable(R.drawable.graybutton));
                    btnRegister.setClickable(false);
                }
            }
        });
    }

    @OnClick({R.id.back, R.id.base_login, R.id.btn_register, R.id.rl_xuanze, R.id.agreement_cmcc})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.base_login:
                startActivity(new Intent(RegisterActivity.this, LoginActivity.class));
                finish();
                break;
            case R.id.btn_register:
                if (flag == 1) {
                    phone = etInputPhone.getText().toString().trim();
                    if (TextUtils.isEmpty(phone) || phone.length() != 11 || !PublicUtil.isMobileNO(phone)) {
                        Toast.makeText(this, "手机号码不正确", Toast.LENGTH_SHORT).show();
                    } else {
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                getPhoneCode(phone);
                            }
                        }).start();
                    }
                } else {
                    showToast("请阅读并点击注册协议");
                }
                break;
            case R.id.rl_xuanze:
                if (flag == 0) {
                    xuanze.setBackground(getResources().getDrawable(R.drawable.duigou));
                    flag = 1;
                } else if (flag == 1) {
                    xuanze.setBackground(getResources().getDrawable(R.drawable.weixuanze));
                    flag=0;
                }
                break;
            case R.id.agreement_cmcc:
                startActivity(new Intent(RegisterActivity.this, RegisterProtocolActivity.class));
                break;
        }
    }

    private void getPhoneCode(final String myphone) {
        OkHttpUtils.post().url(sms_url).addParams("phone",myphone).addParams("type","0").build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                Log.e("xuException", e.getMessage());
                showToast(e.getMessage());
            }

            @Override
            public void onResponse(String response, int id) {
                Log.e("xuxux", response);

                try {
                    JSONObject jsonObject = new JSONObject(response);
                    int data = Integer.parseInt(jsonObject.optString("status"));
                    Log.e("data", response);
                    if (data == 7) {
                        showToast("单手机号短信发送时间间隔少于1分钟");
                        return;
                    }
                    if (data == 2) {
                        showToast("手机号码已注册");
                        return;
                    }
                    if (data == 0) {
                        showToast("发送成功，请注意查收短信");
                        Intent intent = new Intent(RegisterActivity.this, Register_smsActivity.class);
                        intent.putExtra("str_phone", myphone);
                        startActivity(intent);
                    } else {
                        showToast("发送失败");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

/*    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
     //   url = Constant.base_url + "/verify/checkSmsVerify";
        WorksSizeCheckUtil.textChangeListener textChangeListener = new WorksSizeCheckUtil.textChangeListener(verificationCode);
        textChangeListener.addAllEditText(etInputPhone);
        WorksSizeCheckUtil.setChangeListener(new WorksSizeCheckUtil.IEditTextChangeListener() {
            @Override
            public void textChange(boolean isHasContent) {

                if (isHasContent) {
                    verificationCode.setBackground(getResources().getDrawable(R.drawable.btn_login2));

                } else {

                    verificationCode.setBackground(getResources().getDrawable(R.drawable.btn_login_getcode));
                    verificationCode.setText("获取验证码");
                    verificationCode.setClickable(true);
                    time.cancel();
                    //time.onFinish();
                }
            }
        });
    }*/


  /*  private void getCode(String phone) {
        OkHttpUtils.post().url(code_url).addParams("phone", phone).addParams("type", "0").build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                Log.e("xuException", e.getMessage());
                showToast(e.getMessage());
            }

            @Override
            public void onResponse(String response, int id) {
                Log.e("xuxux", response);
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    int data = Integer.parseInt(jsonObject.optString("status"));
                    Log.e("data", response);
                    if (data == 7) {
                        showToast("单手机号短信发送时间间隔少于1分钟");
                        return;
                    }
                    if (data == 2) {
                        showToast("手机号码已注册");
                        return;
                    }
                    if (data == 0) {
                        time.start();
                        showToast("发送成功，请注意查收短信");
                    } else {
                        showToast("发送失败");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }*/

/*    private void register(final String phone, String password) {
        OkHttpUtils.post().url(url).addParams("phone", phone).addParams("code", password).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                Log.e("xuregister", e.getMessage());
            }

            @Override
            public void onResponse(String response, int id) {
                Log.e("XXX", response);
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    int status = Integer.parseInt(jsonObject.optString("status"));

                    if (status == 0) {
                        Intent intent = new Intent(RegisterActivity.this, RegisterActivity_pwdActivity.class);
                        intent.putExtra("str", phone);
                        startActivity(intent);
                    } else {
                        Toast.makeText(RegisterActivity.this, "验证码错误", Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });
    }*/

    /**
     * 计时器
     */
/*    class TimeCount extends CountDownTimer {
        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);//参数依次为总时长,和计时的时间间隔
        }

        @Override
        public void onFinish() {
            //计时完毕时触发
            verificationCode.setText("获取验证码");
            verificationCode.setClickable(true);
            verificationCode.setBackground(getResources().getDrawable(R.drawable.btn_login_getcode));
        }

        @Override
        public void onTick(long millisUntilFinished) {//计时过程显示
            if (verificationCode != null) {
                verificationCode.setClickable(false);
                verificationCode.setBackground(getResources().getDrawable(R.drawable.btn_login2));
                verificationCode.setText(millisUntilFinished / 1000 + "秒");
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        time.cancel();
    }*/
}
